package demo4;
//Description:常用的三种接口
import java.util.Arrays;
import java.util.Comparator;

/*
用哪个接口取决于 业务 推荐Comparator（比较器）
三个常用接口：
Comparable：这个借口有很大的缺点  对类的侵入性非常强  一旦写好了 不能轻易改动
Comparator：比较器  比较灵活
Cloneable：
 */
//class Student implements Comparable<Student>{//<>泛型  里面放类
//    public int age;
//    public String name;
//    public double score;
//    public Student(int age, String name, double score) {
//        this.age = age;
//        this.name = name;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "age=" + age +
//                ", name='" + name + '\'' +
//                ", score=" + score +
//                '}';
//    }
//
//    @Override
//    public int compareTo(Student o) {
////        if(this.score > o.score){
////            return 1;
////        }else if(this.score == o.score){
////            return 0;
////        }else{
////            return -1;
////        }
//        //return this.age - o.age;//从小到大排序
//        return o.age - this.age;//从大到小排序
//        //return this.name.CompareTo(o.name);
//    }
//}
public class Test {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student(12,"h",98);
        students[1] = new Student(19,"hh",97.5);
        students[2] = new Student(17,"hhh",60);
        //System.out.println(students[1].compareTo(students[0]));//谁调用这个方法谁就是this
        //Arrays.sort(students);//这里比较的就是compareTo里面的东西了 默认从小到大
        System.out.println(Arrays.toString(students));
//        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        //Arrays.sort(students,ageComparator);
        Arrays.sort(students,scoreComparator);
        //System.out.println(ageComparator.compare(students[0],students[1]));
        System.out.println(Arrays.toString(students));
    }
}
class ScoreComparator implements  Comparator<Student>{//分数比较器  比较灵活 对类的侵入性非常弱

    @Override
    public int compare(Student o1, Student o2) {

        return (int)(o1.score - o2.score);
    }
}
class NameComparator implements Comparator<Student>{//姓名比较器

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
class AgeComparator implements Comparator<Student>{//年龄比较器

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class Student  {//<>泛型  里面放类
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }


}